import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
// 1. history模式: createWebHistory()   http://xxx/user
// 2. hash模式: createWebHashHistory()  http://xxx/#/user

// vite 的配置 import.meta.env.BASE_URL 是路由的基准地址，默认是 ’/‘
// https://vitejs.dev/guide/build.html#public-base-path

// 如果将来你部署的域名路径是：http://xxx/my-path/user
// vite.config.ts  添加配置  base: my-path，路由这就会加上 my-path 前缀了
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHistory('/'),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') },
    {
      path: '/',
      component: () => import('@/views/Layout/LayoutContainer.vue'),
      redirect: '/content/workexpression',
      meta: {
        title: '影视分析数据可视化平台',
      },
            children: [
        {
          path: '/feel/speakexpression',
          component: () => import('@/views/feel/SpeakExpression.vue'),
          meta: {
            title: '口碑情绪感知',
          },
        },
        {
          path: '/user/userevent',
          component: () => import('@/views/user/UserEvent.vue'),
          meta: {
            title: '用户行为挖掘',
          },
        },
        {
          path: '/content/workexpression',
          component: () => import('@/views/content/WorkExpression.vue'),
          meta: {
            title: '作品综合表现',
          },
        },
        {
          path: '/produce/contentproduce',
          component: () => import('@/views/produce/ContentProduce.vue'),
          meta: {
            title: '内容产出跟踪',
          },
        }
      ]
    }
  ]
})
router.beforeEach((to) => {
  const userStore = useUserStore()
  // 如果没有token则是访问的非登录页，然乎返回
  if (!userStore.token && to.path !== '/login') return '/login'
})
export default router
